<?php
namespace app\common\model;

use app\common\model\BaseModel;
use think\Db;
use chrome\ChromePhp;//引入chromephp插件
use common\Common;//公共函数

/**
 * 基础配置业务
 * @name LibBaseConfig.php
 * @package cws
 * @category model
 * @link http://www.chanekeji.com
 * @author cx
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2018-12-19
 */
class LibBaseConfig extends BaseModel{
	protected $m_config;

	/**
	 * 构造函数
	 */
	public function __construct(){
		$this->m_config = db('base_config');
		parent::__construct();
	}
	
	 /**
	 * 根据配置列表修改
	 * @param array $configList kev=>value list
	 * @return array $result
	 */
	public function updateConfigList($configList){
		try{
			foreach($configList as  $key =>$value){
				$result = $this->m_config->where(array("item_key" => $key))->update(array("item_value" => $value));
			}
			return Common::errorArray(0, "修改成功", true);
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}

	/**
	 * 根据配置列表修改
	 * @param array $configList kev=>value list
	 * @return array $result
	 */
	public function updateConfig($conditions,$row){
		try{
			$result = $this->m_config->where($conditions)->update($row);
			
			if(true == $result){
				return Common::errorArray(0, "修改成功",$result);
			}else{
				return Common::errorArray(1, "修改失败", $result);
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	/**
	 * 获取所有配置用户页面配置元素提取
	 * @return array $result
	 */
	public function getConfigForGet(){
		try {
			$reslut = Db::query("SELECT * FROM base_config WHERE type != 'hidden' ORDER BY sort ASC");
			if($reslut){
				return Common::errorArray(0, "查询成功",$reslut);
			}else{
				return Common::errorArray(1, "查询为空", $reslut);
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	/**
	 * 通过键名获取配置值
	 * @param string $key 键名
	 * @return array $result
	 */
	public function getConfigValue($key){
		try {
			$reslut = $this->m_config ->where(array('item_key'=>$key))-> find();
			if(true == $reslut){
				return Common::errorArray(0, "查询成功",$reslut['item_value']);
			}else{
				return Common::errorArray(1, "查询为空", '');
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	/**
	 * 获取多个配置
	 * @param array $condition
	 * @return array $result
	 */
	public function findAllConfig($condition){
		try {
			$reslut = $this->m_config ->where($condition)->select();
			if(true == $reslut){
				return Common::errorArray(0, "查询成功",$reslut);
			}else{
				return Common::errorArray(1, "查询为空",$reslut);
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	/**
	 * 获取单个配置
	 * @param array $condition
	 * @return array $result
	 */
	public function findConfig($condition){
		try {
			$reslut = $this->m_config ->where($condition)->find();
			if(true == $reslut){
				return Common::errorArray(0, "查询成功",$reslut);
			}else{
				return Common::errorArray(1, "查询为空",$reslut);
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	/**
	 * 添加配置
	 * @param array $row
	 * @return array $result
	 */
	public function addConfig($row){
		try {
			$keyResult = $this->m_config->where(array('item_key'=>$row['item_key']))->find();
			if($keyResult){
				return Common::errorArray(1, "key不能重复", false);
			}
			//$this->clearCache();//添加时，清除对应模块缓存
			$addId = $this->m_config ->insertGetId($row);
			if(true == $addId){
				return Common::errorArray(0, "添加成功",$addId);
			}else{
				return Common::errorArray(1, "添加失败", false);
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	/**
	 * 获取所有配置key=>value
	 * @return array $result
	 */
	public function findConfigKeyValue($type = 'all'){
		try {
			//$sql = "SELECT * FROM base_config ORDER BY sort ASC";
			if($type == 'all'){
			    $sql = "SELECT * FROM base_config WHERE type != 'hidden'  ORDER BY sort ASC";
			}else{
			    $sql = "SELECT * FROM base_config WHERE type != 'hidden' AND type = '{$type}'  ORDER BY sort ASC";
			}
			$reslut = Db::query($sql);
			foreach ($reslut as $per){
				$config[$per['item_key']] = $per['item_value'];
			}
			if(true == $reslut){
				return Common::errorArray(0, "查询成功",$config);
			}else{
				return Common::errorArray(1, "查询为空", $config);
			}
		}catch (Exception $ex){
		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);
		    return  Common::errorArray(1, "数据库操作失败",$ex);
		}
	}
	
	
}


?>